﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Chapter1_15
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            Task<int>[] t = new Task<int>[3];

            t[0] = Task.Run(() => {
                Thread.Sleep(2000);
                return 2000;
            });

            t[1] = Task.Run(() => {
                Thread.Sleep(1000);
                return 1000;
            });

            t[2] = Task.Run(() => {
                Thread.Sleep(3000);
                return 3000;
            });


            while (t.Length > 0)
            {
                int i = Task.WaitAny(t);
                Task<int> completa = t[i];

                Console.WriteLine("A Task que esperava {0} milisegundos terminou.", completa.Result);

                var temp = t.ToList();
                temp.RemoveAt(i);
                t = temp.ToArray(); 
            }
            Console.ReadLine(); 


        }
    }
}
